package totoCharts;
import guestbook.abstractRequestServlet;
import java.util.List;
import javax.servlet.http.*;

/*
 * this servlet is used for updaing the totoCharts object in the DB with users'
 * clicks (for the ask the crowd feature)
 */

@SuppressWarnings("serial")
public class totoChartsUpdate extends abstractRequestServlet<totoGeneralFillout> {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		
		int row;
		int col;
		int update;
		int params;
		int[] newArray;
		params= Integer.parseInt(req.getQueryString());
		
		try
		{
			//if the row number is 1-9
			if (req.getQueryString().length()==3){
				row=params%10;
				col=(params/10)%10;
				update=params/100;			
			}
			else //if the row number is 10-16
			{
				row=params%100;
				col=(params/100)%10;
				update=params/1000;	
			}
			
			//this part gets the totoFeneralFillout object from the DB
			List<totoGeneralFillout> list=getData(totoGeneralFillout.class);
		    
			for (totoGeneralFillout fillout : list) {
		    	
		    	//this part updates the relevant field in totoGeneralFillout
		    	if (col==0){
		    		newArray=fillout.getAway();
		    		if (update==1) newArray[row]++;
		    		else newArray[row]--;
		    		fillout.awayReplace(true, newArray);
		    	}
		    	
		    	if (col==1){
		    		newArray=fillout.getDraw();
		    		if (update==1) newArray[row]++;
		    		else newArray[row]--;
		    		fillout.drawReplace(true, newArray);
		    	}
		    	
		    	if (col==2){
			    		newArray=fillout.getHome();
			    		if (update==1) newArray[row]++;
			    		else newArray[row]--;
			    		fillout.homeReplace(true, newArray);
		    	}
		    	
		    }
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	    finally
	    {
	    	pmClose();
	    }
		
		
	}
}
